import $ from "jquery"
import * as Cesium from "cesium"
import cesiumExt from "../uitls/cesiumExt/cesiumExt"
import IconL1 from "./img/redCircle2.png"
import './earlyWarning.css'
const earlyWarning = function(viewer){
    this.viewer = viewer
    this.warningType = ["selectMark warning1","selectMark warning2","selectMark warning3","selectMark warning4","selectMark warning5"]
    this.warningTypelable = [
        "",
        "蓝色预警",
        "黄色预警",
        "橙色预警",
        "红色预警"
    ]
}
earlyWarning.prototype = {
    _addWarningPoint:function(latlng,level,content){
        var self = this
        //获取经纬度位置的高度
        var position =  Cesium.Cartographic.fromDegrees(latlng.lon,latlng.lat);
        var promise = Cesium.sampleTerrain(self.viewer.terrainProvider,12,[position]);
        Cesium.when(promise, function(updatedPositions) {
            var height = updatedPositions[0].height;
            var hpoint = new Cesium.Cartesian3.fromDegrees(latlng.lon,latlng.lat,height);
            var oneDiv =  document.createElement("div");
            oneDiv.className = self.warningType[level] || this.warningType[0];
            var lable = document.createElement("span");
            lable.innerHTML=self.warningTypelable[level]
            oneDiv.appendChild(lable);
            oneDiv.onclick=function(evt){
              self._openinfoWindow({position:hpoint},content)
            }
            var ViewDom = self.viewer.container
            ViewDom.appendChild(oneDiv);
            cesiumExt.creatHtmlElement(self.viewer,$(oneDiv),hpoint,[0,0],true);
        });
    },
    _openinfoWindow:function(position,content){
        var self = this
        var infoTmep =  '<div class="line"></div>'+
            '<div class="main">'+
            '</div>'
        var InfoWinDom = document.getElementById("cesium-infoWindows")
        if(!InfoWinDom){
            InfoWinDom = document.createElement("div")
            InfoWinDom.innerHTML = infoTmep
            InfoWinDom.className = "ysc-dynamic-layer"
            InfoWinDom.id = "cesium-infoWindows"
            var ViewDom = self.viewer.container
            ViewDom.appendChild(InfoWinDom);
        }
        var elment = $(InfoWinDom);
        var data={
            layerId:"layer1",//英文，且唯一,内部entity会用得到
            position:position.position,
            element:elment
        };
        cesiumExt.showDynamicLayer(self.viewer,data,function (){ //回调函数 改变弹窗的内容;
            elment.find(".main").html(content);
        });
    },
    stationWarningToMap:function(stationList){
        var self = this
        this.clearAll()
        stationList.forEach(station => {
            self._addWarningPoint(station.position,station.level,station.content)
        });
    },
    //清除所有的报警站点
    clearAll:function(){
        $(".selectMark").remove();
        // console.log(selectMark)
        // selectMark.forEach(mark=>{
        //     mark.remove();
        // })
        // $(".selectMark").remove();
    }
}
export default earlyWarning